#region Namespace Inclusions
using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading;
using System.Xml;
using SCI_actuator.Properties;
#endregion


namespace SCI_actuator
{
    class xmlrw
    {
        private XmlDocument doc;
        private string servoid;
        private string path;
        public xmlrw()
        {
            doc = new XmlDocument();
            path = System.Windows.Forms.Application.StartupPath;
            doc.Load(path + "/doc/sci.xml");
            servoid = Read_ServoID();
        }
        #region  portname
        public string Read_Portname()
        {
            XmlNodeList portname;
            portname = doc.GetElementsByTagName("COM");
            return portname.Item(0).InnerText.ToString();
        }

        public void Write_Portname(string portname)
        {
            XmlNodeList port;         
            port = doc.GetElementsByTagName("COM");
            port.Item(0).InnerText = portname;
            doc.Save(path + "/doc/sci.xml");
        }
        #endregion
        #region  stopbits
        public string Read_Stopbits()
        {
            XmlNodeList stopbits;
            stopbits = doc.GetElementsByTagName("stopbits");
            return stopbits.Item(0).InnerText.ToString();
        }

        public void Write_Stopbits(string stopbits)
        {
            XmlNodeList stop;
            stop = doc.GetElementsByTagName("stopbits");
            stop.Item(0).InnerText = stopbits;
            doc.Save(path + "/doc/sci.xml");
        }
        #endregion
        #region  databits
        public string Read_Databits()
        {
            XmlNodeList databits;
            databits = doc.GetElementsByTagName("databits");
            return databits.Item(0).InnerText.ToString();
        }

        public void Write_Databits(string databits)
        {
            XmlNodeList data;
            data = doc.GetElementsByTagName("databits");
            data.Item(0).InnerText = databits;
            doc.Save(path + "/doc/sci.xml");
        }
        #endregion
        #region  parity
        public string Read_Parity()
        {
            XmlNodeList parity;
            parity = doc.GetElementsByTagName("parity");
            return parity.Item(0).InnerText.ToString();
        }

        public void Write_Parity(string parity)
        {
            XmlNodeList par;
            par = doc.GetElementsByTagName("parity");
            par.Item(0).InnerText = parity;
            doc.Save(path + "/doc/sci.xml");
        }
        #endregion
        #region  baudrate
        public string Read_Baudrate()
        {
            XmlNodeList baudrate;
            baudrate = doc.GetElementsByTagName("baudrate");
            return baudrate.Item(0).InnerText.ToString();
        }

        public void Write_Baudrate(string baudrate)
        {
            XmlNodeList baud;
            baud = doc.GetElementsByTagName("baudrate");
            baud.Item(0).InnerText = baudrate;
            doc.Save(path + "/doc/sci.xml");
        }
        #endregion
        #region  datamode
        public string Read_Datamode()
        {
            doc.Load(path + "/doc/sci.xml");  
            XmlNodeList datamode;
            datamode = doc.GetElementsByTagName("datamode");
            return datamode.Item(0).InnerText.ToString();
        }

        public void Write_Datamode(string datamode)
        {
            XmlNodeList mode;
            mode = doc.GetElementsByTagName("datamode");
            mode.Item(0).InnerText = datamode;
            doc.Save(path + "/doc/sci.xml");
        }
        #endregion
        #region  servo ID
        public string Read_ServoID()
        {
            XmlNodeList servoID;
            servoID = doc.GetElementsByTagName("servoID");
            try
            {
                
                return servoID.Item(0).InnerText.ToString();
            }
            catch (System.Exception e)
            {
                return servoid;
            }
        }

        public void Write_ServoID(string servoID)
        {
            
            XmlNodeList servo;
            servo = doc.GetElementsByTagName("servoID");
            servo.Item(0).InnerText = servoID;
            doc.Save(path + "/doc/sci.xml");
            servoid = servoID;
        }
        #endregion
    }
}
